# Contributor: Holger Jaekel <holger.jaekel@gmx.de>
# Maintainer: Holger Jaekel <holger.jaekel@gmx.de>
pkgname=gdcm
pkgver=3.0.22
pkgrel=1
pkgdesc="Grassroots DiCoM is a C++ library for DICOM medical files"
url="http://gdcm.sourceforge.net"
# armhf, aarch64: dcmtk missing
# s390x: vtk missing
# riscv64: blocked by texlive -> luajit
arch="all !armhf !aarch64 !s390x !riscv64"
license="BSD-3-Clause"
makedepends="
	charls-dev
	cmake
	cmake
	dcmtk-dev
	docbook-xsl
	doxygen
	expat-dev
	font-freefont
	fontconfig-dev
	graphviz
	json-c-dev
	libogg-dev
	libtheora-dev
	libxml2-dev
	libxslt-dev
	mesa-dev
	openjpeg-dev
	openssl-dev
	poppler-dev
	python3-dev
	samurai
	sqlite-dev
	swig
	util-linux-dev
	vtk-dev
	"
subpackages="$pkgname-dev"
# gdcmData.tar.bz2 is broken
source="gdcm-$pkgver.tar.bz2::https://sourceforge.net/projects/gdcm/files/gdcm%203.x/GDCM%20$pkgver/gdcm-$pkgver.tar.bz2/download
	http://downloads.sourceforge.net/project/gdcm/gdcmData/gdcmData/gdcmData.tar.gz
	10-docbook4.patch
	20-charls.patch
	30-off_t.patch
	40-java.patch
	50-use_copyright.patch
	60-fix_export_variables.patch
	"

# Optional dependency texlive is not available on ppc64le
case "$CARCH" in
	ppc64le)
		_with_doc="OFF"
		;;
	*)
		makedepends="$makedepends texlive texmf-dist-latexextra texlive-dvi"
		subpackages="$subpackages $pkgname-doc-html $pkgname-doc-pdf"
		_with_doc="ON"
		;;
esac
subpackages="$subpackages $pkgname-doc py3-$pkgname:py3"

# Optional dependency openjdk8-jdk is not available on riscv64
case "$CARCH" in
	riscv64)
		_with_java="OFF"
		;;
	*)
		makedepends="$makedepends java-common openjdk8-jdk"
		subpackages="$subpackages java-$pkgname:java"
		_with_java="ON"
		;;
esac
subpackages="$subpackages lib$pkgname:lib"

prepare() {
	default_prepare

	# Remove bundled utilities (we use Alpine's ones)
	rm -rf Utilities/gdcmexpat
	rm -rf Utilities/gdcmopenjpeg
	rm -rf Utilities/gdcmopenjpeg
	rm -rf Utilities/gdcmzlib
	rm -rf Utilities/gdcmuuid
	rm -rf Utilities/gdcmcharls

	# Remove bundled utilities (we don't use them)
	rm -rf Utilities/getopt
	rm -rf Utilities/pvrg
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		local crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=ON \
		-DCMAKE_BUILD_TYPE=None \
		-DGDCM_INSTALL_PACKAGE_DIR=lib/cmake/gdcm \
		-DGDCM_BUILD_TESTING:BOOL=ON \
		-DGDCM_DATA_ROOT=../gdcmData/ \
		-DGDCM_BUILD_EXAMPLES:BOOL=ON \
		-DGDCM_DOCUMENTATION:BOOL=$_with_doc \
		-DGDCM_WRAP_PYTHON:BOOL=ON \
		-DPYTHON_EXECUTABLE="$(which python3)" \
		-DGDCM_INSTALL_PYTHONMODULE_DIR="$(python -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')" \
		-DGDCM_WRAP_JAVA:BOOL=$_with_java \
		-DGDCM_WRAP_CSHARP:BOOL=OFF \
		-DGDCM_BUILD_SHARED_LIBS:BOOL=ON \
		-DGDCM_BUILD_APPLICATIONS:BOOL=ON \
		-DGDCM_USE_VTK:BOOL=ON \
		-DGDCM_USE_SYSTEM_CHARLS:BOOL=ON \
		-DGDCM_USE_SYSTEM_EXPAT:BOOL=ON \
		-DGDCM_USE_SYSTEM_OPENJPEG:BOOL=ON \
		-DGDCM_USE_SYSTEM_ZLIB:BOOL=ON \
		-DGDCM_USE_SYSTEM_UUID:BOOL=ON \
		-DGDCM_USE_SYSTEM_LJPEG:BOOL=OFF \
		-DGDCM_USE_SYSTEM_OPENSSL:BOOL=ON \
		-DGDCM_USE_JPEGLS:BOOL=ON \
		-DGDCM_USE_SYSTEM_LIBXML2:BOOL=ON \
		-DGDCM_USE_SYSTEM_JSON:BOOL=ON \
		-DGDCM_USE_SYSTEM_POPPLER:BOOL=ON \
		-DJAVA_HOME=/usr/lib/jvm/default-jvm \
 		-DPYTHON_VERSION_MAJOR=3 \
		$crossopts
	cmake --build build
}

check() {
	local test_excludes="(TestMD5$|TestSHA1$|TestWriter$|TestAnonymizer4$|TestImageChangeTransferSyntax1$|TestImageChangeTransferSyntax2$|TestImageChangeTransferSyntax3$|TestImageChangeTransferSyntax4$|TestImageChangeTransferSyntax6$|TestImageChangeTransferSyntax7$|TestPrinter1$|TestImageReader$|TestImageRegionReader1$|TestImageRegionReader2$|TestImageRegionReader3$|TestImageReaderRandomEmpty$|TestImageWriter$|TestCleaner4$|TestJSON1$|TestImageChangeTransferSyntax5$|TestUIDGeneratorPython4$|TestDCMTKMD5Python$|TestSCUValidation$|TestEcho$|TestFind$|TestWrapPython$|gdcmscu-echo-dicomserver$|gdcmscu-store-dicomserver$|gdcmscu-find-dicomserver$|TestStrictScanner1$|TestStrictScanner2_1$|TestStrictScanner2$|TestStrictScanner2_2$|TestDICOMDIRGenerator1$|TestUIDGeneratorPython$|TestvtkGDCMImageWriter1$|TestvtkGDCMMetaImageWriter$|TestvtkGDCMImageReader2_2$|TestvtkGDCMMetaImageWriter2$|TestFileChangeTransferSyntax1$|TestFileChangeTransferSyntax2$|TestFileChangeTransferSyntax2$|TestFileChangeTransferSyntax4$|TestReaderCanRead$|TestRescaler2$)"

	ctest --test-dir build --output-on-failure -E $test_excludes
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

html() {
	pkgdesc="Grassroots DiCoM HTML documentation"
	amove usr/share/doc/gdcm-3.0/html
}

pdf() {
	pkgdesc="Grassroots DiCoM PDF documentation"
	amove usr/share/doc/gdcm-3.0/gdcm-$pkgver.pdf
}

java() {
	pkgdesc="$pkgdesc (Java bindings)"
	mkdir -p "$subpkgdir"/usr/share/java
	mv "$pkgdir"/usr/lib/gdcm.jar "$subpkgdir"/usr/share/java/gdcm-$pkgver.jar
	cd "$subpkgdir"/usr/share/java/
	ln -s gdcm-$pkgver.jar gdcm.jar
	amove usr/lib/libgdcmjni.so
}

py3() {
	pkgdesc="$pkgdesc (Python3 bindings)"

	amove usr/lib/python3*
}

lib() {
	pkgdesc="Grassroots DiCoM runtime libraries"
	amove usr/lib
	amove usr/share/gdcm*

}

sha512sums="
47b7d562153789ec23d858f0084d138f46fac661cb306932e7c9beb11e133ad958035bfd2d356d64133d10708e66ca2bb6f4eea13b533b007a6db8343a73e91c  gdcm-3.0.22.tar.bz2
ddc67a7a0b41b2d6e1e03defb6fb8a06ceeb2e9dd9bb47dfa4f7283d79df7c1e52577b799c00e930719ae6cdda46bded9497a67b8241359f238f5366085ec9ee  gdcmData.tar.gz
87fcf6ea8f6eb34e27bf2ecf5cbd4501092d1f936341dfcf75085780da75fab9be7809521d5cb00a6c4a9e512bfbe7de20233104b19fab245391632e409498a3  10-docbook4.patch
1f26d24e9786e42808e3937538d4115ceed6978190269a8c7781a9784720b6f7f5986b66dc320f788a0178b77b7cf6e64ac5ee0607c0a6fbe9c7659356f20614  20-charls.patch
15520cd087ea401744d46a55e83eb307742ee4b94256476531b9f209be4ffaa2b0653d4ab8266b8c82282a52fceaa1f887d5fdebf83c1da0551c51335f74a08e  30-off_t.patch
3254dd27821f9239c0379d8f9ca577a6f2a7ff14b55701cca9fb0ac47568c24d1e681bc404f1d97a9ed8ee00f2a11701c7d53eee7186e5ce9f7ca8eca7834326  40-java.patch
da032530bd27b47ab4ec46652c4fcb65dda02ddeca4a3175f49cbf46efd43a1ff75b126475a694ce2df944e3970c67e378b1cf4db4cacf40d87bd999d37b4eed  50-use_copyright.patch
6994893b96069874bacc9285b994c7f93314b9570c7dd8b64ee51bb701fd5c8bd9892641e3b9d2d6f11fa4f2b7aebbbe25d72c1e689b5cccad34280f4c5199a5  60-fix_export_variables.patch
"
